//
//  TRHomeViewController.m
//  SafPay
//
//  Created by  ztryxh on 2018/6/25.
//  Copyright © 2018年 ebo. All rights reserved.
//

#import "TRHomeViewController.h"
#import "UIButton+Layout.h"
#import "HomeViewCell.h"
#import "CustomerController.h"
#import "SettingViewController.h"
#import "WalletDetailsController.h"
#import "WalletModel.h"
#import "WalletCoinListModel.h"
#import "ChoseCoinController.h"
#import "CopySafeWordController.h"
#import "TRHomeViewModel.h"
@interface TRHomeViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    
    UILabel *moneyLab;
    UIButton *showBtn;
    UILabel *subLab;
    UIImageView *img;
}
@property (nonatomic , strong)dispatch_source_t timer;
@property (strong,nonatomic) UITableView *tableView;
@property (strong,nonatomic) UILabel *bottomView;
@property (strong,nonatomic) UIImageView *headerView;
@property (strong,nonatomic) UIButton *footerView;
@property (strong,nonatomic) NSMutableArray *arrDS;
@property (strong,nonatomic) TRRequest *request;
@property (strong,nonatomic) WalletModel *currentModel;
@property (strong,nonatomic) TRHomeViewModel *viewModel;
@end

@implementation TRHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - system
-(void)updateViewConstraints{
    
    kWeakSelf(weakSelf);
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.equalTo(weakSelf.view);
        make.top.equalTo(weakSelf.view).offset(0);
    }];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(weakSelf.view);
        make.height.mas_equalTo(40);
    }];
    [self.footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(weakSelf.bottomView.mas_top).mas_offset(-10);
        make.left.equalTo(weakSelf.view).offset(22);
        make.right.equalTo(weakSelf.view).offset(-22);
        make.height.mas_equalTo(44);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view).offset(22);
        make.right.equalTo(weakSelf.view).offset(-22);
        make.top.mas_equalTo(weakSelf.headerView.mas_bottom);
        make.bottom.mas_equalTo(weakSelf.footerView.mas_top).mas_offset(-10);
    }];
}
#pragma mark - Privacy
-(void)tr_addSubviews{
    
    img = [UIImageView new];
    img.image = kImageName(@"icon_bg");
    [self.view addSubview:img];
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.footerView];
}
-(void)tr_bindViewModel{
    
    
}
-(void)tr_layoutNavigation{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavHeight)];
    [self.view addSubview:view];
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(15, kNavHeight - 37, 30, 30)];
    [leftBtn setImage:kImageName(@"icon_customer") forState:UIControlStateNormal];
    [[leftBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        CustomerController *vc = [CustomerController new];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [view addSubview:leftBtn];
    
    UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth - 45, kNavHeight - 37, 30, 30)];
    [rightBtn setImage:kImageName(@"icon_setting") forState:UIControlStateNormal];
    [[rightBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        SettingViewController *vc = [SettingViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [view addSubview:rightBtn];
    
    UILabel *titleLab = [UILabel labelWithFrame:CGRectMake(0, 0, 200, 20) font:kFONT(17) color:[UIColor whiteColor] text:@"管家云银包"];
    titleLab.yzCenterX = kScreenWidth/2;
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.yzCenterY = leftBtn.yzCenterY;
    [view addSubview:titleLab];
    
}

#pragma mark -Lazy
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.rowHeight = 69;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"HomeViewCell" bundle:nil] forCellReuseIdentifier:@"HomeViewCell"];
    }
    return _tableView;
}
- (UILabel *)bottomView
{
    if (!_bottomView) {
        
        _bottomView = [UILabel labelWithFont:kFONT(14) color:rgb(230,46,46) text:@"未备份钱包存在风险，请立即备份 >"];
        _bottomView.backgroundColor = [UIColor whiteColor];
        _bottomView.textAlignment = NSTextAlignmentCenter;
        [_bottomView setUserInteractionEnabled:YES];
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
        [_bottomView addGestureRecognizer:tap];
        
    }
    return _bottomView;
}
- (UIImageView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIImageView alloc]initWithFrame:CGRectMake(4, kNavHeight + 18, kScreenWidth - 8, 206)];
        _headerView.image = kImageName(@"icon_card");
        _headerView.userInteractionEnabled = YES;
        showBtn = [UIButton new];
        [showBtn setImage:kImageName(@"icon_hidden") forState:UIControlStateNormal];
        [showBtn setImage:kImageName(@"icon_show") forState:UIControlStateSelected];
        
        moneyLab = [UILabel labelWithFont:kFONT(33) color:rgb(55,166,212) text:[NSString stringWithFormat:@"¥ %@",self.currentModel.count_cny]];
        [[showBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            self->showBtn.selected = !self->showBtn.selected;
            self->moneyLab.text = self->showBtn.selected?@"****":[NSString stringWithFormat:@"¥ %@",self.currentModel.count_cny];
        }];
        moneyLab.textAlignment = NSTextAlignmentCenter;
        
        UIButton *guzhiBtn = [UIButton new];
        [guzhiBtn setTitle:@"总资产估值" forState:UIControlStateNormal];
        guzhiBtn.titleLabel.font = kFONT(14);
        [guzhiBtn setTitleColor:rgb(153,153,153) forState:UIControlStateNormal];
        guzhiBtn.layer.cornerRadius = 12;
        guzhiBtn.layer.borderColor = rgb(153,153,153).CGColor;
        guzhiBtn.layer.borderWidth = 1;
        
        subLab = [UILabel labelWithFont:kFONT(13) color:rgb(117,117,117) text:@"刚刚更新\n..."];
        subLab.numberOfLines = 0;
        subLab.textAlignment = NSTextAlignmentCenter;
        __weak typeof (UIView *)weakHeader = _headerView;
        [_headerView addSubview: showBtn];
        [_headerView addSubview: moneyLab];
        [_headerView addSubview: guzhiBtn];
        [_headerView addSubview: subLab];
        [showBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakHeader).offset(-40);
            make.top.equalTo(weakHeader).offset(20);
        }];
        
        [moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakHeader);
            make.top.equalTo(weakHeader).offset(59);
        }];
        
        [guzhiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(25);
            make.centerX.equalTo(weakHeader);
            make.top.mas_equalTo(self->moneyLab.mas_bottom).mas_offset(16);
        }];
        
        [subLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakHeader);
            make.bottom.equalTo(weakHeader).offset(-16);
        }];
    }
    return _headerView;
}
- (UIButton *)footerView
{
    if (!_footerView) {
        _footerView = [UIButton buttonWithType:UIButtonTypeCustom];
        _footerView.layer.cornerRadius = 3;
        _footerView.layer.borderColor = rgb(153,153,153).CGColor;
        _footerView.titleLabel.font = kFONT(16);
        _footerView.layer.borderWidth = 1;
        [_footerView setTitleColor:rgb(95,96,96) forState:UIControlStateNormal];
        [_footerView setTitle:@"添加其它币种" forState:UIControlStateNormal];
        [_footerView setImage:kImageName(@"icon_add") forState:UIControlStateNormal];
        [_footerView sc_setLayoutStyle:SCButtonLayoutStyleImageLeft spacing:5];
        [_footerView addTarget:self action:@selector(addCoinClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _footerView;
}
-(TRHomeViewModel *)viewModel{
    
    if(!_viewModel){
        
        _viewModel=[TRHomeViewModel new];
        return _viewModel;
        
    }
    return _viewModel;
}
@end
